define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    require('md5');
    require('cookie');
    var template = require('template');
    dateFormat(template);

    $('#form-login').validate({
        rules: {
            accountName: {
                required: true,
                minlength: 2,
                maxlength: 20
            },
            password: {
                required: true,
                minlength: 2,
                maxlength: 22
            }
        },
        messages: {
            accountName: "请填写",
            nickName: "请填写"

        },
        success: function () {
            login();
            return false;
        }
    });

    function login() {
        var accountName = $.trim($('#accountName').val());
        var password = $.md5($.trim($('#password').val()));
        $('.user-login').prop('disabled', 'disabled');
        $.post('/login/checkLogin', {accountName: accountName, password: password}, function (data) {
            switch (data.code) {
                case 0:
                    var url = data.data;
                    password = $.trim($('#password').val());
                    if ($("#remember-me").prop('checked')) {
                        $.cookie("accountName", accountName, {expires: 7}); // 存储一个带7天期限的 cookie
                        $.cookie("password", password, {expires: 7});
                    } else {
                        $.cookie("accountName", accountName, {expires: -1}); // 删除cookie
                        $.cookie("password", password, {expires: -1});
                    }
                    window.location.href = url;
                    break;
                default:
                    toastError(data.msg);
                    $('.user-login').removeAttr('disabled');
            }
        });
    }

    function getNewGame() {
        var data = {
            page: 1,
            rows: 10
        };
        $.post('/login/listNewGame', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var html = template('tp-new', {list: list});
                    $('#tab-new tbody').empty();
                    $('#tab-new tbody').html(html);
                    break;
                default:
                // toastError('查询数据失败');
            }
        });
    }

    function getPopularGame() {
        var data = {
            page: 1,
            rows: 10
        };
        $.post('/login/listPopularGame', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var html = template('tp-new', {list: list});
                    $('#tab-popular tbody').empty();
                    $('#tab-popular tbody').html(html);
                    break;
                default:
                // toastError('查询数据失败');
            }
        });
    }

    function getServiceGame() {
        var data = {
            page: 1,
            rows: 10
        };
        $.post('/login/listServiceGame', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var html = template('tp-service', {list: list});
                    $('#tab-service tbody').empty();
                    $('#tab-service tbody').html(html);
                    break;
                default:
                // toastError('查询数据失败');
            }
        });
    }

    function dateFormat(template) {
        template.helper('dateFormat', function (date, format) {
            date = new Date(date);
            var map = {
                "M": date.getMonth() + 1, //月份
                "d": date.getDate(), //日
                "h": date.getHours(), //小时
                "m": date.getMinutes(), //分
                "s": date.getSeconds(), //秒
                "q": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
                var v = map[t];
                if (v !== undefined) {
                    if (all.length > 1) {
                        v = '0' + v;
                        v = v.substr(v.length - 2);
                    }
                    return v;
                }
                else if (t === 'y') {
                    return (date.getFullYear() + '').substr(4 - all.length);
                }
                return all;
            });
            return format;
        });
    }

    if ($.cookie("accountName") != null && $.cookie("accountName") != undefined) {
        $("#remember-me").attr("checked", true);
        $("#accountName").val($.cookie("accountName"));
        $("#password").val($.cookie("password"));
    }

    function initData() {
        getNewGame();
        getPopularGame();
        getServiceGame();
    }

    initData();
    
});